Imports System.ComponentModel

Public Class frmSimpleFile
  Inherits System.Windows.Forms.Form

  Dim isLocked As Boolean = False
  Friend WithEvents chkFilenameEnc As System.Windows.Forms.CheckBox

#Region " Windows Form Designer generated code "

  Public Sub New()
    MyBase.New()
    'This call is required by the Windows Form Designer.
    InitializeComponent()

    Me.tbPass.PasswordChar = IIf(CryptorMain.IsRunningXP(), Microsoft.VisualBasic.ChrW(9679), "*"c)
    Me.tbPassAgain.PasswordChar = IIf(CryptorMain.IsRunningXP(), Microsoft.VisualBasic.ChrW(9679), "*"c)
  End Sub

  Friend WithEvents bgWorker1 As EncryptionWorker
  Public ReadOnly Property EncryptionWorker() As EncryptionWorker
    Get
      Return bgWorker1
    End Get
  End Property


  Friend isOK As Boolean = True

  'Form overrides dispose to clean up the component list.
  Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
      If Not (components Is Nothing) Then
        components.Dispose()
      End If
    End If
    MyBase.Dispose(disposing)
  End Sub

  'Required by the Windows Form Designer
  Private components As System.ComponentModel.IContainer

  'NOTE: The following procedure is required by the Windows Form Designer
  'It can be modified using the Windows Form Designer.  
  'Do not modify it using the code editor.
  Friend WithEvents tbPass As System.Windows.Forms.TextBox
  Friend WithEvents lblEnterPass As System.Windows.Forms.Label
  Friend WithEvents bEncrypt As System.Windows.Forms.Button
  Friend WithEvents lblInfo As System.Windows.Forms.Label
  Friend WithEvents lblReEnterPass As System.Windows.Forms.Label
  Friend WithEvents tbPassAgain As System.Windows.Forms.TextBox
  Friend WithEvents bCancel As System.Windows.Forms.Button
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(frmSimpleFile))
    Me.tbPass = New System.Windows.Forms.TextBox
    Me.lblEnterPass = New System.Windows.Forms.Label
    Me.bEncrypt = New System.Windows.Forms.Button
    Me.lblInfo = New System.Windows.Forms.Label
    Me.lblReEnterPass = New System.Windows.Forms.Label
    Me.tbPassAgain = New System.Windows.Forms.TextBox
    Me.bCancel = New System.Windows.Forms.Button
    Me.bgWorker1 = New Cryptor.EncryptionWorker
    Me.chkFilenameEnc = New System.Windows.Forms.CheckBox
    Me.SuspendLayout()
    '
    'tbPass
    '
    Me.tbPass.AccessibleDescription = Nothing
    Me.tbPass.AccessibleName = Nothing
    resources.ApplyResources(Me.tbPass, "tbPass")
    Me.tbPass.BackgroundImage = Nothing
    Me.tbPass.Font = Nothing
    Me.tbPass.Name = "tbPass"
    '
    'lblEnterPass
    '
    Me.lblEnterPass.AccessibleDescription = Nothing
    Me.lblEnterPass.AccessibleName = Nothing
    resources.ApplyResources(Me.lblEnterPass, "lblEnterPass")
    Me.lblEnterPass.Font = Nothing
    Me.lblEnterPass.Name = "lblEnterPass"
    '
    'bEncrypt
    '
    Me.bEncrypt.AccessibleDescription = Nothing
    Me.bEncrypt.AccessibleName = Nothing
    resources.ApplyResources(Me.bEncrypt, "bEncrypt")
    Me.bEncrypt.BackgroundImage = Nothing
    Me.bEncrypt.Font = Nothing
    Me.bEncrypt.Name = "bEncrypt"
    '
    'lblInfo
    '
    Me.lblInfo.AccessibleDescription = Nothing
    Me.lblInfo.AccessibleName = Nothing
    resources.ApplyResources(Me.lblInfo, "lblInfo")
    Me.lblInfo.Name = "lblInfo"
    '
    'lblReEnterPass
    '
    Me.lblReEnterPass.AccessibleDescription = Nothing
    Me.lblReEnterPass.AccessibleName = Nothing
    resources.ApplyResources(Me.lblReEnterPass, "lblReEnterPass")
    Me.lblReEnterPass.Font = Nothing
    Me.lblReEnterPass.Name = "lblReEnterPass"
    '
    'tbPassAgain
    '
    Me.tbPassAgain.AccessibleDescription = Nothing
    Me.tbPassAgain.AccessibleName = Nothing
    resources.ApplyResources(Me.tbPassAgain, "tbPassAgain")
    Me.tbPassAgain.BackgroundImage = Nothing
    Me.tbPassAgain.Font = Nothing
    Me.tbPassAgain.Name = "tbPassAgain"
    '
    'bCancel
    '
    Me.bCancel.AccessibleDescription = Nothing
    Me.bCancel.AccessibleName = Nothing
    resources.ApplyResources(Me.bCancel, "bCancel")
    Me.bCancel.BackgroundImage = Nothing
    Me.bCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel
    Me.bCancel.Font = Nothing
    Me.bCancel.Name = "bCancel"
    '
    'bgWorker1
    '
    Me.bgWorker1.Encrypt = False
    Me.bgWorker1.FilenameEncrypted = False
    Me.bgWorker1.InputFile = Nothing
    Me.bgWorker1.OutputFile = Nothing
    Me.bgWorker1.Result = Cryptor.EncryptionWorker.ReturnType.Well
    Me.bgWorker1.WorkerReportsProgress = True
    '
    'chkFilenameEnc
    '
    Me.chkFilenameEnc.AccessibleDescription = Nothing
    Me.chkFilenameEnc.AccessibleName = Nothing
    resources.ApplyResources(Me.chkFilenameEnc, "chkFilenameEnc")
    Me.chkFilenameEnc.BackgroundImage = Nothing
    Me.chkFilenameEnc.Checked = Global.Cryptor.My.MySettings.Default.FilenameEncrypted
    Me.chkFilenameEnc.CheckState = System.Windows.Forms.CheckState.Checked
    Me.chkFilenameEnc.DataBindings.Add(New System.Windows.Forms.Binding("Checked", Global.Cryptor.My.MySettings.Default, "FilenameEncrypted", True, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged))
    Me.chkFilenameEnc.Font = Nothing
    Me.chkFilenameEnc.Name = "chkFilenameEnc"
    Me.chkFilenameEnc.UseVisualStyleBackColor = True
    '
    'frmSimpleFile
    '
    Me.AcceptButton = Me.bEncrypt
    Me.AccessibleDescription = Nothing
    Me.AccessibleName = Nothing
    resources.ApplyResources(Me, "$this")
    Me.BackgroundImage = Nothing
    Me.CancelButton = Me.bCancel
    Me.Controls.Add(Me.chkFilenameEnc)
    Me.Controls.Add(Me.bCancel)
    Me.Controls.Add(Me.lblReEnterPass)
    Me.Controls.Add(Me.tbPassAgain)
    Me.Controls.Add(Me.tbPass)
    Me.Controls.Add(Me.lblInfo)
    Me.Controls.Add(Me.bEncrypt)
    Me.Controls.Add(Me.lblEnterPass)
    Me.Font = Nothing
    Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
    Me.MaximizeBox = False
    Me.MinimizeBox = False
    Me.Name = "frmSimpleFile"
    Me.ResumeLayout(False)
    Me.PerformLayout()

  End Sub

#End Region

  Private Sub bEncrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bEncrypt.Click
    '//Sanity checks...
    If Me.tbPass.Text <> Me.tbPassAgain.Text AndAlso bgWorker1.Encrypt Then
      MessageBox.Show("The two passwords entered do not match! Please re-enter the password", "Invalid Password", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
      LockUI(True)
      Return
    End If
    If Me.tbPass.Text.Length < 8 AndAlso bgWorker1.Encrypt Then
      MessageBox.Show("The password is less than 8 characters long." & IIf(bgWorker1.Encrypt, " This is not long enough to properly secure the file.", "") & vbCrLf & IIf(bgWorker1.Encrypt, "Please enter a longer one", "Please enter the password you used to encrypt the file"), "Invalid Password", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
      LockUI(True)
      Return
    End If
    ' if we have a file to process do not close
    If Not String.IsNullOrEmpty(Me.bgWorker1.InputFile) Then
      bgWorker1.Initialise(tbPass.Text)
      bgWorker1.RunWorkerAsync()
      LockUI(True)
    Else
      LockUI(False)
      Me.DialogResult = Windows.Forms.DialogResult.OK
      Me.Close()
    End If
  End Sub

  Private Sub tbPass_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbPass.TextChanged, tbPassAgain.TextChanged
    Me.bEncrypt.Enabled = Me.tbPass.Text <> "" AndAlso (Me.tbPassAgain.Text <> "" OrElse Not bgWorker1.Encrypt)
  End Sub

  Private Sub bCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bCancel.Click
    If isLocked Then
    Else
      Me.Close()
      Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End If
  End Sub

  Sub SetDecryptUI()
    bgWorker1.Encrypt = False
    'Me.Text = "Decrypt File '" & IO.Path.GetFileName(file) & "'"
    ' Me.lblEnterPass.Text = "Enter password to decrypt file with:"
    Me.lblInfo.Visible = False
    Me.bEncrypt.Text = My.Resources.Decrypt
    Me.tbPassAgain.Enabled = False
    Me.tbPassAgain.Visible = False
    Me.lblReEnterPass.Visible = False
    'Me.Size = New Size(398, 130)
  End Sub

  Private Sub LockUI(ByVal lock As Boolean)
    Me.bEncrypt.Enabled = Not lock
    'Me.tbPass.Enabled = Not lock
    'Me.tbPassAgain.Enabled = Not lock AndAlso bgWorker1.Encrypt
    isLocked = lock
  End Sub

  Public Property Password() As String
    Get
      Return tbPass.Text
    End Get
    Set(ByVal value As String)
      tbPass.Text = value
    End Set
  End Property


  Private Sub bgWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bgWorker1.ProgressChanged
    If Me.InvokeRequired Then
      Me.Invoke(New ProgressChangedEventHandler(AddressOf bgWorker1_ProgressChanged), sender, e)
    Else
      Dim prog As Integer = e.ProgressPercentage
      Me.Text = IIf(bgWorker1.Encrypt, "Encrypting... ", "Decrypting... ") & IIf(prog > 100, 100, prog) & "%"
    End If
  End Sub

  Private Sub bgWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgWorker1.RunWorkerCompleted
    Dim retType As EncryptionWorker.ReturnType = CType(e.Result, EncryptionWorker.ReturnType)

    Select Case retType
      Case EncryptionWorker.ReturnType.Well
        MessageBox.Show(String.Format(My.Resources.Donewell, _
         IIf(bgWorker1.Encrypt, My.Resources.encr, My.Resources.decr)), _
         Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information)
        LockUI(False)
        Me.Close()
      Case EncryptionWorker.ReturnType.Badly
        MessageBox.Show(String.Format(My.Resources.Donewell, _
         IIf(bgWorker1.Encrypt, My.Resources.encr, My.Resources.decr)), _
         Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        LockUI(False)
        Me.Close()
      Case EncryptionWorker.ReturnType.IncorrectPassword
        LockUI(False)
        Me.tbPass.Clear()
        Me.tbPass.Focus()
        Me.tbPass.Select()
        MessageBox.Show(Me, "The password you used was incorrect. Please use the password you used to encrypt the file.", "Incorrect Password", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    End Select
  End Sub

  Private Sub frmSimpleFile_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

    e.Cancel = isLocked
    If Not e.Cancel Then
      My.Settings.Password = tbPass.Text
      My.Settings.Save()
    End If
  End Sub
End Class